package com.zjdiepu.www.biz.partner;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.partner.ThInsteadPayListMapper;
import com.zjdiepu.www.ibiz.partner.IThInsteadPayListService;
import com.zjdiepu.www.model.partner.dto.ThInsteadPayList;
import com.zjdiepu.www.model.partner.ext.ThInsteadPayListExt;

/**
 * 第三方代偿信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年10月11日 14时54分<br/>
 * @since 1.0<br/>
 */
@Service
public class ThInsteadPayListServiceImpl extends BaseService implements IThInsteadPayListService {
	public static final Logger logger = LoggerFactory.getLogger(ThInsteadPayListServiceImpl.class);

	@Autowired
	private ThInsteadPayListMapper thInsteadPayListMapper;
	
	@Override
	public ThInsteadPayList saveOrUpdateThInsteadPayList(String busiId, 
			Date repayDate, BigDecimal repayFee, Integer repayStatus, BigDecimal demandMarginFee,
			String busiType, String xeApplyNo, String currentUserId,
			String currentUserName) {
		ThInsteadPayList thInsteadPayListParam = new ThInsteadPayList();
		thInsteadPayListParam.setDpStatus(DpStatus.NORMAL.getCode());
		thInsteadPayListParam.setBusiId(busiId);
		thInsteadPayListParam.setRepayDate(repayDate);
		ThInsteadPayList thInsteadPayList = thInsteadPayListMapper.selectRefined(thInsteadPayListParam);
		boolean insert = false;
		if(null == thInsteadPayList){
			insert = true;
			thInsteadPayList = new ThInsteadPayList();
			thInsteadPayList.initInsertData(currentUserId, currentUserName);
		} else {
			thInsteadPayList.initUpdateData(currentUserId, currentUserName);
		}
		thInsteadPayList.setBusiType(busiType);
		thInsteadPayList.setBusiId(busiId);
		thInsteadPayList.setThirdId(xeApplyNo);
		thInsteadPayList.setRepayDate(repayDate);
		thInsteadPayList.setRepayFee(repayFee);
		thInsteadPayList.setRepayStatus(repayStatus);
		thInsteadPayList.setDemandMarginFee(demandMarginFee);

		if(insert) {
			thInsteadPayListMapper.insert(thInsteadPayList);
		} else {
			thInsteadPayListMapper.updateByPrimaryKeySelective(thInsteadPayList);
		}
		return thInsteadPayList;
	}
	
	@Override
	public void saveUpdateAndDelete(ThInsteadPayList thInsteadPayList, OperateType operType) {
		switch (operType) {
		case add:
			thInsteadPayList.initInsertData(thInsteadPayList.getModifier(), thInsteadPayList.getModifierName());
			thInsteadPayListMapper.insert(thInsteadPayList);
			break;
		case edit:
			thInsteadPayList.initUpdateData(thInsteadPayList.getModifier(), thInsteadPayList.getModifierName());
			// 影响的记录数
			int updateCount = thInsteadPayListMapper.updateByPrimaryKeySelective(thInsteadPayList);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			thInsteadPayList.initDeleteData(thInsteadPayList.getModifier(), thInsteadPayList.getModifierName());
			// 影响的记录数
			int deleteCount = thInsteadPayListMapper.updateByPrimaryKeySelective(thInsteadPayList);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ThInsteadPayList> selectByExt(ThInsteadPayListExt thInsteadPayListExt) {
		return thInsteadPayListMapper.selectByExt(thInsteadPayListExt);
	}
	
	@Override
	public int countByExt(ThInsteadPayListExt thInsteadPayListExt){
		return thInsteadPayListMapper.countByExt(thInsteadPayListExt);
	}
	
	@Override
	public ThInsteadPayList selectById(String id) {
		return thInsteadPayListMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			thInsteadPayListMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ThInsteadPayList thInsteadPayList) {
		return thInsteadPayListMapper.countRefined(thInsteadPayList);
	}

	@Override
	public ThInsteadPayList selectRefined(ThInsteadPayList thInsteadPayList) {
		return thInsteadPayListMapper.selectRefined(thInsteadPayList);
	}
	
	@Override
	public int deletedRefined(ThInsteadPayList thInsteadPayList) {
		return thInsteadPayListMapper.deletedRefined(thInsteadPayList);
	}

	@Override
	public boolean repeat(ThInsteadPayList thInsteadPayList) {
		int count = countRefined(thInsteadPayList);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(thInsteadPayList.getId())) {
			return true;
		}
		ThInsteadPayList dbItem = selectRefined(thInsteadPayList);
		if(null == dbItem) {
			return false;
		}
		return !thInsteadPayList.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return thInsteadPayListMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<ThInsteadPayList> thInsteadPayLists) {
		return thInsteadPayListMapper.insertBatch(thInsteadPayLists);
	}
}
